﻿using System;
using System.CodeDom;
using System.Collections.Generic;
using System.ComponentModel.Design.Serialization;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Kugar.Core.ExtMethod;

namespace Kugar.UI.WinForm
{
    public class TotalColumnRowBaseCodeDom : CodeDomSerializer
    {
        public override object Deserialize(IDesignerSerializationManager manager, object codeObject)
        {
            //MessageBox.Show("D1");
            CodeDomSerializer baseClassSerializer = (CodeDomSerializer)manager.
            GetSerializer(typeof(TotalColumnRowBase).BaseType, typeof(CodeDomSerializer));
            //MessageBox.Show("D2");
            return baseClassSerializer.Deserialize(manager, codeObject);
        }

        public override object Serialize(IDesignerSerializationManager manager, object value)
        {

            var baseClassSerializer = (CodeDomSerializer)manager.GetSerializer(typeof(TotalColumnRowBase).BaseType, typeof(CodeDomSerializer));

            object codeObject = baseClassSerializer.Serialize(manager, value);

            if (codeObject is CodeStatementCollection)
            {
                var statements = (CodeStatementCollection)codeObject;

                var targetName = manager.GetName(value);

                var myCom = (TotalColumnRowBase)manager.GetInstance(targetName);

                //manager.GetInstance(targetName).FastGetValue("")


                if (statements.Count > 0)
                {
                    //MessageBox.Show("S1");

                    var tempList = new List<CodeStatement>();

                    foreach (var s in statements)
                    {
                        if (s is CodeExpressionStatement)
                        {
                            var t1 = (CodeExpressionStatement)s;

                            if (!(t1.Expression is CodeMethodInvokeExpression))
                            {
                                continue;
                            }

                            var t2 = (CodeMethodInvokeExpression)t1.Expression;

                            if (t2.Method.MethodName.EndsWith("Columns.Add"))
                            {
                                tempList.Add((CodeStatement)s);
                            }
                        }
                        else if (s is CodeAssignStatement)
                        {
                            var t1 = (CodeAssignStatement)s;

                            if (!(t1.Left is CodePropertyReferenceExpression))
                            {
                                continue;
                            }

                            var t2 = (CodePropertyReferenceExpression)t1.Left;

                            if (t2.PropertyName == "Columns")
                            {
                                tempList.Add((CodeStatement)s);
                            }
                        }
                    }

                    foreach (var o in tempList)
                    {
                        statements.Remove(o);
                    }

                    //statements.Remove((s) =>
                    //                  {
                    //                      if (s is CodeExpressionStatement)
                    //                      {
                    //                          var t1 = (CodeExpressionStatement)s;

                    //                          if (!(t1.Expression is CodeMethodInvokeExpression))
                    //                          {
                    //                              return false;
                    //                          }

                    //                          var t2 = (CodeMethodInvokeExpression)t1.Expression;

                    //                          if (t2.Method.MethodName.EndsWith("Columns.Add"))
                    //                          {
                    //                              return true;
                    //                          }
                    //                      }
                    //                      else if (s is CodeAssignStatement)
                    //                      {
                    //                          var t1 = (CodeAssignStatement)s;

                    //                          if (!(t1.Left is CodePropertyReferenceExpression))
                    //                          {
                    //                              return false;
                    //                          }

                    //                          var t2 = (CodePropertyReferenceExpression)t1.Left;

                    //                          if (t2.PropertyName == "Columns")
                    //                          {
                    //                              return true;
                    //                          }
                    //                      }

                    //                      return false;

                    //                  });
                    //MessageBox.Show("S2");
                }

                //删除系统对Columns属性生成的所有操作

                const string commentText = "添加自定义的Columns属性操作";
                var comment = new CodeCommentStatement(commentText);

                statements.Add(comment);

                if (myCom.Columns.Count > 0)
                {
                    foreach (var keypair in myCom.Columns)
                    {
                        var keyPairKey = new CodeFieldReferenceExpression(new CodeThisReferenceExpression(), keypair.Key.Name);

                        var keyPairValue = new CodeObjectCreateExpression("Kugar.UI.WinForm.TotalColumnConfig", new CodeExpression[] { new CodePrimitiveExpression(keypair.Value.FormatString) });

                        var methodInvoke = new CodeMethodInvokeExpression(
                            // targetObject that contains the method to invoke.
                                                                                                    new CodeThisReferenceExpression(),
                            // methodName indicates the method to invoke.
                                                                                                    targetName + ".Columns.Add",
                            // parameters array contains the parameters for the method.
                                                                                                    new CodeExpression[] { keyPairKey, keyPairValue });

                        statements.Add(new CodeExpressionStatement(methodInvoke));
                    }
                }


            }
            return codeObject;
        }
    }
}
